package org.siam.impl.schemas {
	
	import flash.utils.Dictionary;
	
	import org.siam.core.schemas.ISchema;
	import org.siam.core.schemas.ISchemaProperty;

	public class Schema implements ISchema {
		
		public static const NULL:ISchema = new SchemaNull();
		
		private var _properties:Dictionary;
		private var _name:String;
		
		public function Schema() {
			_properties = new Dictionary();
		}
		
		public function get name():String {
			return _name;
		}
		
		public function findProperty(name:String):ISchemaProperty {
			return _properties.hasOwnProperty(name) 
					? _properties[name] : SchemaProperty.NULL;
		}
		
		public function get propertyNames():Array {
			var names:Array = new Array();
			for (var name:String in _properties) {
				names.push(name);
			}
			return names;
		}
		
		public static function newInstance(configuration:XML):ISchema {
			var schema:Schema = new Schema();
			var schemaName:String = configuration["@name"];
			schema._name = schemaName;
			for each (var propertyXML:XML in configuration["property"]) {
				var property:ISchemaProperty = SchemaProperty.newInstance(propertyXML);
				schema.addProperty(property);
			}
			
			return schema;
		}
		
		private function addProperty(property:ISchemaProperty):void {
			_properties[property.name] = property;	
		}
	}
}